﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Domain.Model.Abstract;

using NHibernate.Validator.Constraints;

namespace Domain.Model
{
    public class FormElement
    {
        #region Types

        public static string TYPE_UNKNOWN   = "typeUnknown";
        public static string TYPE_NUMERIC   = "typeNumeric";
        public static string TYPE_CHAR      = "typeChar";
        public static string TYPE_DATE      = "typeDate";

        #endregion

        #region Properties

        [NotNull]
        public virtual string Name { get; set; }

        [NotNull]
        public virtual int Position { get; set; }

        [NotNull]
        public virtual string Type { get; set; }

        [NotNull]
        public virtual Form Form { get; set; }

        #endregion

        #region Fields

        private int? oldHashCode;

        #endregion

        #region Constructor

        public FormElement(string name, int position, string type)
        {
            Name = name;
            Position = position;
            Type = type;
        }

        public FormElement()
            : this("unknownFormElement", -1, TYPE_UNKNOWN)
        {
        
        }

        #endregion

        #region Overrides

        public override int GetHashCode()
        {
            if (oldHashCode.HasValue)
                return oldHashCode.Value;

            var thisIsTransient = Equals(Name, "unknownFormElement");

            if (thisIsTransient)
            {
                oldHashCode = base.GetHashCode();
                return oldHashCode.Value;
            }

            return Name.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            var other = obj as FormElement;

            if (other == null)
                return false;

            var otherIsTransient = Equals(other.Name, "unknownFormElement");
            var thisIsTransient = Equals(Name, "unknownFormElement");

            if (otherIsTransient && thisIsTransient)
                return ReferenceEquals(other, this);

            return other.Name.Equals(Name);
        }

        #endregion
    }
}
